package eolica

import br.leiam.eolica.facade.*
import br.leiam.eolica.excecoes.EolicaException;
import javax.jws.*
import java.util.*
import java.text.SimpleDateFormat;
import eolica.*

@WebService(targetNamespace="http://eolica.embedded.ufcg.edu.br") 
class EolicaService {
	
	private static int WIND_GRAPH = 1;
	private static int STATISTICAL_GRAPH = 2;

    //CONFIGURACOES
	boolean transactional = true
	static expose=['xfire']

    //ATRIBUTOS
	FacadeEolica facade = FacadeEolica.getInstance();
	/*
	//#########  METODOS NOVOS
	@WebMethod(operationName = "gerarGraficoVelocidade")
	@WebResult(name="graficoVelocidade") 
	String[]  gerarGraficoVelocidade(
	@WebParam(name = "estacao") String estacao, 
	@WebParam(name = "altura") int altura,
	@WebParam(name = "dataInicial") String dataInicial, 
	@WebParam(name = "dataFinal")  String dataFinal) throws EolicaException {
		
		return facade.gerarGraficoVelocidade(estacao, altura, dataInicial, dataFinal);
	}
	
	@WebMethod(operationName = "gerarGraficoVelocidade2")
	@WebResult(name="graficoVelocidade") 
	String[] gerarGraficoVelocidade2(
	@WebParam(name = "estacao") String estacao, 
	@WebParam(name = "altura") int altura,
	@WebParam(name = "dataInicial") String dataInicial, 
	@WebParam(name = "dataFinal")  String dataFinal, 
	@WebParam(name = "alturaImagem") int imageHeight,
	@WebParam(name = "larguraImagem") int imageWidth) throws EolicaException {
		
		return facade.gerarGraficoVelocidade(estacao, altura, dataInicial, dataFinal, imageHeight, imageWidth);
	}
	
	@WebMethod(operationName = "gerarGraficoRosaDosVentosEWeibull")
	@WebResult(name="graficoRosaDosVentosEWeibull") 
	String[] gerarGraficoRosaDosVentosEWeibull(
	@WebParam(name = "estacao") String estacao, 
	@WebParam(name = "altura") int altura,
	@WebParam(name = "dataInicial") String dataInicial, 
	@WebParam(name = "dataFinal")  String dataFinal) throws EolicaException {
		
		return facade.gerarGraficoRosaDosVentosEWeibull(estacao, altura, dataInicial, dataFinal);
	}
	
	@WebMethod(operationName = "gerarGraficoRosaDosVentosEWeibull2")
	@WebResult(name="graficoRosaDosVentosEWeibull") 
	String[] gerarGraficoRosaDosVentosEWeibull2(
	@WebParam(name = "estacao") String estacao, 
	@WebParam(name = "altura") int altura,
	@WebParam(name = "dataInicial") String dataInicial, 
	@WebParam(name = "dataFinal")  String dataFinal, 
	@WebParam(name = "alturaImagem") int imageHeight,
	@WebParam(name = "larguraImagem") int imageWidth) throws EolicaException {
		
		return facade.gerarGraficoRosaDosVentosEWeibull(estacao, altura, dataInicial, dataFinal,imageHeight,imageWidth);
	}
	
	private void autenticarUsuario(String usuario, String senha) throws EolicaException{
		//String USERNAME = "eolica";
		//String PASSWORD = "!jdUhV";
		//def u = User.findByUserNameAndPasswd(user, DU.shaHex(password))
		def u = User.findByUserNameAndPasswd(usuario, senha)
		if(!u)
			throw new EolicaException("Login ou senha invalida")
	}
	
	*/
	
	//#########  METODO ANTIGO
	
	@WebMethod(operationName = "getGraphics")
	def @WebResult(name="graphics") String[] getGraphics(
	@WebParam(name = "user") String user,
	@WebParam(name = "password") String password,
	@WebParam(name = "station") String station,
	@WebParam(name = "height") int height,
	@WebParam(name = "infoType") String infoType,
	@WebParam(name = "startperiod") long startPeriod,
	@WebParam(name = "endPeriod") long endPeriod,
	@WebParam(name = "imageHeight") int imageHeight,
	@WebParam(name = "imageWidth") int imageWidth) throws IOException {
		/*
		//Testa se o usuario e senha existem
		autenticarUsuario(user, password)
		//BEGIN CODIGO COLADO
		def u = User.findByUserNameAndPasswd(user, DU.shaHex(password))
		if(!u)
			throw new Exception("Login ou senha inv‡lida")
		
		if(!infoType.equals("1")&&!infoType.equals("2"))
			throw new Exception("Tipo de informa‹o n‹o dispon’vel")
	
		if(height!= 12 && height!= 70 && height!=100 && height!=102)
		  throw new Exception("Altura inv‡lida")
		  
		if(imageHeight < 1 || imageWidth < 1)
			throw new Exception("Tamanho inv‡lido da imagem")
		//FIM CODIGO COLADO
		*/
		System.out.println("Metodo getGraphics: "+ "String user "+user+" password "+password+" station "+station+" height "+height+" infoType "+infoType+" startPeriod "+startPeriod+" endPeriod "+endPeriod+" imageHeight "+imageHeight+" imageWidth "+imageWidth);
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
			def beforeDate = sdf2.format(new Date(startPeriod))
			def today = sdf2.format(new Date(endPeriod))
		
		
		//Formata as datas
		SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
		def dataInicial = formater.format(new Date(startPeriod))
		def dataFinal = formater.format(new Date(endPeriod))
		def tipoGrafico = Integer.parseInt(infoType)
		
		if (tipoGrafico == WIND_GRAPH) {
			return facade.gerarGraficoVelocidade(station, height, dataInicial, dataFinal, imageHeight, imageWidth);// OK
		} else if (tipoGrafico == STATISTICAL_GRAPH) {
			 return facade.gerarGraficoRosaDosVentosEWeibull(station, height, dataInicial, dataFinal, imageHeight, imageWidth);// OK			
		} else throw new EolicaException("Tipo de informacao "+tipoGrafico+ " nao disponivel")
	}
}
